package com.hyperboost.backend.service.impl;

import java.util.List;

import com.hyperboost.backend.domain.OpportunityCategory;
import com.hyperboost.backend.mapper.OpportunityCategoryMapper;
import com.hyperboost.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.hyperboost.backend.mapper.OpportunityMapper;
import com.hyperboost.backend.domain.Opportunity;
import com.hyperboost.backend.service.IOpportunityService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 机会管理Service业务层处理
 * 
 * @author LXY
 * @date 2025-04-08
 */
@Service
public class OpportunityServiceImpl implements IOpportunityService 
{
    @Autowired
    private OpportunityMapper opportunityMapper;

    @Autowired
    private OpportunityCategoryMapper opportunityCategoryMapper;

    /**
     * 查询机会管理
     * 
     * @param opportunityId 机会管理主键
     * @return 机会管理
     */
    @Override
    public Opportunity selectOpportunityByOpportunityId(Long opportunityId)
    {
        return opportunityMapper.selectOpportunityByOpportunityId(opportunityId);
    }

    /**
     * 查询机会管理列表
     * 
     * @param opportunity 机会管理
     * @return 机会管理
     */
    @Override
    public List<Opportunity> selectOpportunityList(Opportunity opportunity)
    {
        return opportunityMapper.selectOpportunityList(opportunity);
    }

    /**
     * 新增机会管理
     * 
     * @param opportunity 机会管理
     * @return 结果
     */
    @Transactional // 开启事务注解
    @Override
    public int insertOpportunity(Opportunity opportunity)
    {
        opportunity.setCreateTime(DateUtils.getNowDate());

        // 新增机会，调用mapper层insertOpportunity()方法
        opportunityMapper.insertOpportunity(opportunity);

        // 同步更新机会分类表的机会数量
        Long categoryId = opportunity.getCategoryId();
        OpportunityCategory opportunityCategory = opportunityCategoryMapper.selectOpportunityCategoryByCategoryId(categoryId);
        opportunityCategory.setOpportunityCount(opportunityCategory.getOpportunityCount() + 1);
        opportunityCategoryMapper.updateOpportunityCategory(opportunityCategory);

        // 返回1，表示新增成功
        return 1;
    }

    /**
     * 修改机会管理
     * 
     * @param opportunity 机会管理
     * @return 结果
     */
    @Transactional // 开启事务注解
    @Override
    public int updateOpportunity(Opportunity opportunity)
    {
        opportunity.setUpdateTime(DateUtils.getNowDate());

        // 获取原机会的分类ID
        Opportunity oldOpportunity = opportunityMapper.selectOpportunityByOpportunityId(opportunity.getOpportunityId());

        // 修改机会，调用mapper层updateOpportunity()方法
        opportunityMapper.updateOpportunity(opportunity);

        // 同步更新机会分类表的机会数量
        Long oldCategoryId = oldOpportunity.getCategoryId();
        Long newCategoryId = opportunity.getCategoryId();
        if (!oldCategoryId.equals(newCategoryId)) {
            // 旧分类计数减1
            OpportunityCategory oldCategory = opportunityCategoryMapper.selectOpportunityCategoryByCategoryId(oldCategoryId);
            oldCategory.setOpportunityCount(oldCategory.getOpportunityCount() - 1);
            opportunityCategoryMapper.updateOpportunityCategory(oldCategory);
            // 新分类计数加1
            OpportunityCategory newCategory = opportunityCategoryMapper.selectOpportunityCategoryByCategoryId(newCategoryId);
            newCategory.setOpportunityCount(newCategory.getOpportunityCount() + 1);
            opportunityCategoryMapper.updateOpportunityCategory(newCategory);
        }

        // 返回1，表示修改成功
        return 1;
    }

    /**
     * 批量删除机会管理
     * 
     * @param opportunityIds 需要删除的机会管理主键
     * @return 结果
     */
    @Transactional // 开启事务注解
    @Override
    public int deleteOpportunityByOpportunityIds(Long[] opportunityIds)
    {
        // 同步更新机会分类表的机会数量
        for (Long opportunityId : opportunityIds) {
            Opportunity opportunity = opportunityMapper.selectOpportunityByOpportunityId(opportunityId);
            Long categoryId = opportunity.getCategoryId();
            OpportunityCategory opportunityCategory = opportunityCategoryMapper.selectOpportunityCategoryByCategoryId(categoryId);
            opportunityCategory.setOpportunityCount(opportunityCategory.getOpportunityCount() - 1);
            opportunityCategoryMapper.updateOpportunityCategory(opportunityCategory);
        }

        // 删除机会，调用mapper层deleteOpportunityByOpportunityIds()方法
        opportunityMapper.deleteOpportunityByOpportunityIds(opportunityIds);

        // 返回删除的记录数，表示删除成功
        return opportunityIds.length;
    }

    /**
     * 删除机会管理信息
     * 
     * @param opportunityId 机会管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteOpportunityByOpportunityId(Long opportunityId)
    {
        // 获取删除的机会分类ID
        Opportunity opportunity = opportunityMapper.selectOpportunityByOpportunityId(opportunityId);
        Long categoryId = opportunity.getCategoryId();

        // 同步更新机会分类表的机会数量
        OpportunityCategory category = opportunityCategoryMapper.selectOpportunityCategoryByCategoryId(categoryId);
        category.setOpportunityCount(category.getOpportunityCount() - 1);
        opportunityCategoryMapper.updateOpportunityCategory(category);

        // 删除机会，调用mapper层deleteOpportunityByOpportunityId()方法
        opportunityMapper.deleteOpportunityByOpportunityId(opportunityId);

        // 返回1，表示删除成功
        return 1;
    }
}
